package model

import (
	"context"
	"grpc-test/rpc"
	"math/rand"
	"time"
)


type CalculationServer struct {}

var _ rpc.CalculationServer = CalculationServer{}

func (s CalculationServer) NumInit(context.Context, *rpc.Empty) (*rpc.NumReq, error) {
	rand.Seed(time.Now().Unix())
	return &rpc.NumReq{
		I: uint32(rand.Int31n(100000)),
	}, nil
}

func (s CalculationServer) Plus(c context.Context, r *rpc.NumReq) (*rpc.NumRes, error) {
	var tmp uint32 = r.I % 10
	var res uint64
	if tmp == 0 {
		res = 5 * (uint64(r.I) << 1 - 9)
	} else {
		for i := r.I - tmp + 1; i <= r.I; i++ {
			res += uint64(i)			
		}
	}
	return &rpc.NumRes{
		I: res,
	}, nil
}
